<?php

// use Illuminate\Support\Facades\Schema;
use Jialeo\LaravelSchemaExtend\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class InitChipUser extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('chip_assets', function (Blueprint $table) {
            $table->comment = '碎片资产表';

            $table->increments('id');
            $table->uuid('uuid')->index() ;

            $table->integer('chip_id')->index()->comment('碎片id');
            $table->integer('user_id')->index()->comment('用户id');

            $table->integer('balance')->unsigned()->default(0)->comment('余额');
            $table->integer('frozen')->unsigned()->default(0)->comment('冻结的余额'); 

            $table->integer('used_total')->default(0)->comment('已使用数量');

            $table->softDeletes();
            $table->timestamps();

        });

        Schema::create('chip_asset_records', function (Blueprint $table) {
            $table->comment = '碎片流水记录';

            $table->increments('id');
            $table->uuid('uuid')->index() ;

            $table->integer('chip_id')->unsigned()->index()->comment('碎片id');
            $table->integer('user_id')->unsigned()->index()->comment('用户id');
            $table->integer('asset_id')->unsigned()->index()->comment('资产id');
            $table->integer('order_id')->unsigned()->nullable()->index()->comment('用户id');

            $table->integer('before')->unsigned()->comment('变动前余额'); 
            $table->integer('after')->unsigned()->comment('变动后余额'); 
            $table->integer('value')->unsigned()->comment('数额'); 

            $table->boolean('type')->default(1)->index()->comment('0为流出   1流入');   // 0为流出   1流入
            $table->string('description', 255)->comment('流水描述'); 

            $table->softDeletes();
            $table->timestamps();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('chip_assets');
        Schema::dropIfExists('chip_asset_records');
    }
}
